package com.migu.contacts.main;

import static com.migu.contacts.net.Service.API;

import android.util.Log;
import android.view.View;

import androidx.fragment.app.Fragment;

import com.flyco.tablayout.listener.CustomTabEntity;
import com.frank.libandroid.BaseAdapter;
import com.frank.libandroid.BaseFragment;
import com.frank.libandroid.BottomPopup;
import com.frank.libandroid.CenterPopup;
import com.frank.libandroid.HttpCallback;
import com.frank.libandroid.TabEntity;
import com.frank.libandroid.butterknife.BindAdapter;
import com.frank.libandroid.butterknife.OnClick;
import com.lxj.xpopup.XPopup;
import com.migu.contacts.R;
import com.migu.contacts.bean.Page;
import com.migu.contacts.bean.TimerBean;
import com.migu.contacts.databinding.FramgentCountdownBinding;
import com.migu.contacts.databinding.ItemCountdownBinding;
import com.migu.contacts.databinding.ItemCountdownFilterBinding;
import com.migu.contacts.databinding.PopCountdownFilerBinding;
import com.migu.contacts.databinding.PopTimerDeleteBinding;
import com.migu.contacts.main.countdown.AddMomentActivity;
import com.migu.contacts.main.countdown.CountDownDoneFragment;
import com.migu.contacts.main.countdown.CountDownImportantFragment;
import com.migu.contacts.main.countdown.CountDownUndoneFragment;
import com.migu.contacts.main.countdown.EditMomentActivity;
import com.migu.contacts.net.ResultCallback;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class CountDownFragment extends BaseFragment<FramgentCountdownBinding> {
    @BindAdapter(R.id.recyclerView)
    BaseAdapter<ItemCountdownBinding, TimerBean> baseAdapter = new BaseAdapter<ItemCountdownBinding, TimerBean>() {
        @Override
        protected void onBindData(ItemCountdownBinding binding, TimerBean bean, int position) {
            super.onBindData(binding, bean, position);
            binding.space.setVisibility(position != mList.size() - 1 ? View.GONE : View.VISIBLE);
        }

        @OnClick(R.id.delete)
        public void delete(TimerBean bean, int position) {
            Log.d(TAG, "delete() called with: bean = [" + bean + "], position = [" + position + "]");
            showDeletePop(bean, position);
        }

        @OnClick(R.id.status)
        public void status(TimerBean bean, int position) {
            Log.d(TAG, "delete() called with: bean = [" + bean + "], position = [" + position + "]");
            int status = bean.status;
            if (status == 1) {
                status = 2;
            } else {
                status = 1;
            }
            int finalStatus = status;
            API.calendarChangeStatus(bean.id, status).enqueue(new ResultCallback<Boolean>() {
                @Override
                protected void onSuccess(Boolean aBoolean) {
                    Log.d(TAG, "onSuccess() called with: aBoolean = [" + aBoolean + "]");
                    if (aBoolean) {
                        CountDownFragment.this.getData();
                    }
                }
            });
        }

        @OnClick(R.id.root)
        public void root(TimerBean bean) {
            go(bean);
        }
    };

    private void go(TimerBean bean) {
//        类型 1=生日 2=节日 3=礼物 4=普通 5=周期
        switch (bean.eventType) {
            case 4:
                EventBus.getDefault().postSticky(bean);
                start(EventDetailActivity.class);
                break;
            case 1:
                EventBus.getDefault().postSticky(bean);
                start(BirthdayCountDownActivity.class);
                break;
            case 2:
                EventBus.getDefault().postSticky(bean);
                start(FestivalCountDownActivity.class);
                break;
            case 3:
                EventBus.getDefault().postSticky(bean);
                start(GiftCountDownActivity.class);
                break;
            case 6:
                start(EditMomentActivity.class, bean);
                break;
            default:
                Log.i(TAG, "go: " + bean.eventType);
        }
    }

    @OnClick(R.id.add_moment)
    public void addMoment() {
        Log.d(TAG, "addMoment() called");
        start(AddMomentActivity.class);
    }

    @OnClick(R.id.showFilter)
    public void showFilter() {
        Log.d(TAG, "addMoment() called");
        showPopNotice();
    }

    public static class CountDownBean {
        public String name;
        public String value;
        public boolean checked;

        public CountDownBean(String name, String value) {
            this.name = name;
            this.value = value;
        }
    }

    private void showPopNotice() {
        BottomPopup bottomPopup = new BottomPopup<PopCountdownFilerBinding>(mActivity) {
            CountDownBean checkedNotice;
            @BindAdapter(R.id.recyclerView)
            BaseAdapter<ItemCountdownFilterBinding, CountDownBean> noticeAdapter = new BaseAdapter<ItemCountdownFilterBinding, CountDownBean>() {
                @OnClick(R.id.checkBox)
                public void checkBox(CountDownBean bean) {
                    Log.d(TAG, "checkBox() called with: bean = [" + bean.checked + "]");
                    if (bean.checked) {
                        checkedNotice = bean;
                        for (CountDownBean noticeBean : list) {
                            noticeBean.checked = false;
                        }
                        bean.checked = true;
                        notifyDataSetChanged();
                    }
                }

                @Override
                protected void onBindData(ItemCountdownFilterBinding binding, CountDownBean bean, int position) {
                    super.onBindData(binding, bean, position);
                    if (position == mList.size() - 1) {
                        binding.divider.setVisibility(GONE);
                    } else {
                        binding.divider.setVisibility(VISIBLE);
                    }
                }
            };

            @OnClick(R.id.close)
            public void quit() {
                dismiss();
            }


            List<CountDownBean> list = new ArrayList<>();

            @Override
            protected void initView() {
                super.initView();
                list.add(new CountDownBean("按提醒时间正序", "asc"));
                list.add(new CountDownBean("按提醒时间倒序", "desc"));
                noticeAdapter.setData(list);
            }

            @OnClick(R.id.confirm)
            public void confirm() {
                filter = checkedNotice.value;
                Log.i(TAG, "confirm: " + filter);
                getData();
                dismiss();
            }

        };
        new XPopup.Builder(mActivity).asCustom(bottomPopup).show();
    }

    String filter = "desc";

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private void showDeletePop(TimerBean bean, int position) {
        CenterPopup bottomPopup = new CenterPopup<PopTimerDeleteBinding>(mActivity) {
            @OnClick(R.id.confirm)
            public void confirm() {
                API.calendarDelete(bean.id).watch(mActivity, new HttpCallback<Boolean>() {
                    @Override
                    protected void onSuccess(Boolean aBoolean) {
                        baseAdapter.getData().remove(position);
                        baseAdapter.notifyDataSetChanged();
                    }
                });
                dismiss();
            }

            @OnClick(R.id.quit)
            public void quit() {
                dismiss();
            }

        };
        new XPopup.Builder(mActivity).asCustom(bottomPopup).show();
    }

    @Override
    protected void initView() {
        super.initView();
        ArrayList<CustomTabEntity> tabEntityList = new ArrayList<>();
        tabEntityList.add(new TabEntity("未完成", 0, 0));
        tabEntityList.add(new TabEntity("已完成", 0, 0));
        tabEntityList.add(new TabEntity("重要时刻", 0, 0));
        ArrayList<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new CountDownUndoneFragment());
        fragmentList.add(new CountDownDoneFragment());
        fragmentList.add(new CountDownImportantFragment());
//        mBinding.tabLayout.setTabData(tabEntityList, getActivity(), R.id.fl_content, fragmentList);
        mBinding.tabLayout.setTabData(tabEntityList);
        mBinding.tabLayout.setOnTabSelectListener(new com.flyco.tablayout.listener.OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                currentPosition = position;
                getData();
                if (position == 2) {
                    mBinding.addMoment.setVisibility(View.VISIBLE);
                } else {
                    mBinding.addMoment.setVisibility(View.GONE);
                }
                mBinding.notice.setVisibility(View.GONE);
                if (position == 0) {
                    mBinding.notice.setVisibility(View.VISIBLE);
                    mBinding.noticeText.setText("待办事项与行事历同步，可在行事历中添加");
                } else if (position == 2) {
                    mBinding.noticeText.setText("可在未完成已完成中标记事项为重要时刻");
                    mBinding.notice.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    @OnClick(R.id.notice)
    public void notice() {
        mBinding.notice.setVisibility(View.GONE);
    }

    int currentPosition = 0;

    private void getData() {
        API.getTimerPage(currentPosition + 1, 1, 99999, filter).watch(mActivity, new HttpCallback<Page<TimerBean>>() {
            @Override
            protected void onSuccess(Page<TimerBean> page) {
                Log.d(TAG, "onSuccess() called with: page = [" + page + "]");
                if (page != null) {
                    baseAdapter.setData(page.list);
                } else {
                    baseAdapter.setData(null);
                }
                if (baseAdapter.getData().isEmpty()) {
                    mBinding.emptyView.setVisibility(View.VISIBLE);
                } else {
                    mBinding.emptyView.setVisibility(View.GONE);
                }

            }
        });
    }

    //1=待执行  2=已执行 3=重要时刻
}
